package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Hashtable;

import au.edu.unsw.editingtool.global.EditingToolGlobalConstant;

public class DBEditingpool extends DBManager {

	protected void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("Publication", "pid");
		MappingTable.put("Editor", "editor");
		MappingTable.put("Status", "status");
		MappingTable.put("Date", "date");
		MappingTable.put("Version", "version");
		MappingTable.put("Reviewer", "reviewer");
	}
	
	public DBEditingpool() {
		this.table = "editingpool";
		this.primaryKeys = new String[]{"Publication"};
		this.setupDBMapping();
	}
	
	public boolean isAuthor(String editor, String pid) throws SQLException, ClassNotFoundException {
		String fields[] = {"Editor"};
		
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.setFieldsCondition("Editor", this.setEqual(editor));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			this.clean();
			return false;
		}
		if (!this.collectResults()) {
			this.clean();
			return false;
		}	
		for (int row = 0; row < Result.length / fields.length; row++) {
			for (int column = 0; column < fields.length; column++) {
				this.clean();
				return true;
			}
		}
		this.clean();
		return false;
	}
	
	public void addNewRecord(Hashtable<String, String> record) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		record.put("Status", EditingToolGlobalConstant.editingpoolStatusEditing);
		record.put("Date", "CURRENT_TIMESTAMP()");
		this.setColumnValues(record);
		
		this.initDB();
		this.insert(false, false);
		this.clean();
	}
	
	public void changeAsSubmit(String pid) throws SQLException, ClassNotFoundException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", setEqual(pid));
		
		Hashtable<String, String> record = new Hashtable<String, String>();
		record.put("Status", EditingToolGlobalConstant.editingpoolStatusSubmit);
		
		this.initDB();
		this.setColumnValues(record);
		this.updateRecords();
		this.clean();
	}
	
	public void changeAsReview(String pid, String reviewer) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", setEqual(pid));
		
		Hashtable<String, String> record = new Hashtable<String, String>();
		record.put("Reviewer", reviewer);
		record.put("Status", EditingToolGlobalConstant.editingpoolStatusReview);
		
		this.initDB();
		this.setColumnValues(record);
		this.updateRecords();
		this.clean();
	}
	
	public void editingAgain(String pid, String version) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", setEqual(pid));
		
		Hashtable<String, String> record = new Hashtable<String, String>();
		record.put("Version", version);
		record.put("Status", EditingToolGlobalConstant.editingpoolStatusEditing);
		
		this.initDB();
		this.setColumnValues(record);
		this.updateRecords();
		this.clean();
	}
	
	public void deleteRecord(String pid) throws ClassNotFoundException, SQLException {
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", this.setEqual(pid));
		
		this.initDB();
		this.deleteRecord();
		this.clean();
	}
	
	public String checkStatus(String pid) throws ClassNotFoundException, SQLException {
		String fields[] = {"Status"};
		this.setTable(this.table, "");
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			this.clean();
			return null;
		}
		if (!this.collectResults()) {
			this.clean();
			return null;
		}
		for (int row = 0; row < Result.length / fields.length; row++) {
			for (int column = 0; column < fields.length; column++) {
				String result = Result[row][column];
				this.clean();
				return result;
			}
		}
		this.clean();
		return null;
	}
	
}
